package org.apnprovider.storage;
import java.util.Collection;
import org.apnprovider.Configuration;
import org.apnprovider.Device;
import org.apnprovider.DeviceStorage;
import com.google.common.collect.Multimap;
import com.google.common.collect.LinkedListMultimap; 
/**
 * Trivial implementation of DeviceStorage interface
 * Implementation is based on java.util.HashMap
 * 
 * @see DeviceStorage
 * 
 * @author Alexey Palamarchuk (slonnik@gmail.com)
 *
 */
public class DeviceStorageTrivial implements DeviceStorage{

	/** devices to store*/
	private Multimap<String, Device> devices = LinkedListMultimap.create();
	
	public final static String PROVIDER_KEYS = "provider.keys";
	/**
	 * ctor
	 * @param config - configuration 
	 */
	public DeviceStorageTrivial(Configuration config)
	{
		String keys = config.get(PROVIDER_KEYS);
		if(keys != null)
		{
			for(String key : keys.split(","))
			{
				String[] configPair = key.split("=");
				addDevice(configPair[0], new Device(configPair[1]));
			}
		}
	}
	
	@Override
	public Collection<Device> findDevicesByProviderKey(String key) {
		if(key == null)
			throw new IllegalArgumentException("key is null");
		
		return devices.get(key);
	}

	@Override
	public void addDevice(String key, Device device) {
		if(key == null)
			throw new IllegalArgumentException("key is null");
		if(device == null)
			throw new IllegalArgumentException("device is null");
		devices.put(key, device);
	}

	
}
